package com.jmu.edu.IMApplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.tabs.TabLayout;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.MainPageViewAdapter;
import com.jmu.edu.IMApplication.event.NotifyToSendMessageEvent;
import com.jmu.edu.IMApplication.event.SwitchFragmentEvent;
import com.jmu.edu.IMApplication.utils.ViewUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ChatFunctionFragment extends Fragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private TextView descriptionText;
    private MainPageViewAdapter adapter;
    private MaterialButton sendButton;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_chat_function, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        viewPager = getView().findViewById(R.id.fragment_chat_function_viewpager);
        tabLayout = getView().findViewById(R.id.fragment_chat_function_tab);
        descriptionText = getView().findViewById(R.id.fragment_chat_function_description);
        sendButton = getView().findViewById(R.id.fragment_chat_function_send);
        initTabLayout();
        initListener();
    }

    private void initListener() {
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前显示的item的下标
                int position = viewPager.getCurrentItem();
                //根据下标发布对应的事件以供各自的adapter订阅
                EventBus.getDefault().post(new NotifyToSendMessageEvent(position));
            }
        });
    }

    public void initTabLayout() {

        //获得要展示在viewpager的fragment列表，由于展示的fragment是固定不变的，所以直接写死在MyApplication,也方便后续在其他界面直接调用
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new AlbumFragment());
        fragmentList.add(new EmojiFragment());


        // 创建一个字符串数组，两个int数组，分别存放TabLayout中对每个Fragment的图标(选中以及未选中)和名称，一一对应
        String[] nameArray = {"相机", "表情", "手势设置"};
        int[] iconSelectedArray = {R.drawable.message_selected, R.drawable.contacts_selected, R.drawable.settings_selected};
        int[] iconUnSelectedArray = {R.drawable.message, R.drawable.contacts, R.drawable.settings};

        // 设置viewPager的适配器
        MainPageViewAdapter adapter = new MainPageViewAdapter(getChildFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        // tabLayout和viewPager联动,tabLayout会自动根据viewPager适配器的getCount，来生成对应数量的tabItem
        tabLayout.setupWithViewPager(viewPager);

        // 往TabLayout里面添加我们需要的自定义布局
        for (int i = 0; i < nameArray.length; i++) {
            int icon;
            if (i != 0) {
                icon = iconUnSelectedArray[i];
            } else {
                icon = iconSelectedArray[i];
            }
            if (i < fragmentList.size()) {
                tabLayout.getTabAt(i).setCustomView(getCustomTabItemView(nameArray[i], icon));
            } else {
                tabLayout.addTab(tabLayout.newTab().setCustomView(getCustomTabItemView(nameArray[i], icon)));
            }

        }


        // 设置选中监听器，实现选中/未选中状态展示不一样的图片
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 获取当前选中的Tab的位置
                int position = tab.getPosition();
                if (position < adapter.getCount()) {
                    // 在viewPager中设置当前展示的Fragment
                    viewPager.setCurrentItem(position);
                    View view = tab.getCustomView();
                    // 将对应的ImageView设置成选中的图标
                    ((ImageView) view.findViewById(R.id.tab_layout_item_icon)).setImageResource(iconSelectedArray[position]);
                } else if (position == adapter.getCount()) {
                    tabLayout.selectTab(tabLayout.getTabAt(0));
                    Fragment fragment = MyApplication.fragmentManager.findFragmentByTag("customText");
                    if (fragment == null) {
                        fragment = new CustomTextFragment();
                    }
                    EventBus.getDefault().post(new SwitchFragmentEvent(fragment, "customText", "chat"));
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 获取当前选中的Tab的位置
                int position = tab.getPosition();
                View view = tab.getCustomView();
                // 将对应的ImageView设置成未选中的图标
                ((ImageView) view.findViewById(R.id.tab_layout_item_icon)).setImageResource(iconUnSelectedArray[position]);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private View getCustomTabItemView(String name, int icon) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_tablayout_item, null);
        TextView nameText = view.findViewById(R.id.tab_layout_item_name);
        ImageView imageView = view.findViewById(R.id.tab_layout_item_icon);
        nameText.setText(name);
        imageView.setImageResource(icon);
        return view;
    }

}
